package com.zxmc.server.controller.admin;


import com.zxmc.common.constant.JwtClaimsConstant;
import com.zxmc.common.context.BaseContext;
import com.zxmc.common.properties.JwtProperties;
import com.zxmc.common.result.Result;
import com.zxmc.common.utils.JwtUtil;
import com.zxmc.pojo.dto.UserLoginDto;
import com.zxmc.pojo.entity.Admin;
import com.zxmc.pojo.entity.User;
import com.zxmc.pojo.vo.UserLoginVO;
import com.zxmc.server.service.AdminService;
import com.zxmc.server.service.UserService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController("adminUserController")
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;
    @GetMapping("/listAll")
    @ApiOperation("查询所有用户信息")
    public Result<List<User>> listAll() {
        log.info("查询所有用户信息");
        return Result.success(userService.listAll());
    }

    @DeleteMapping("/delete/{username}")
    @ApiOperation("删除用户")
    public Result delete(@PathVariable String username) {
        log.info("删除用户：{}", username);
        userService.delete(username);
        return Result.success();
    }
    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public Result update(@RequestBody User user) {
        log.info("修改用户信息:{}", user);
        userService.updateOnly(user);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("查询管理员信息")
    public Result<Admin> list() {
        log.info("查询管理员信息");
        return Result.success(adminService.list());
    }
    @PostMapping("/register")
    @ApiOperation("管理员注册")
    public Result register(@RequestBody UserLoginDto userLoginDto) {
        log.info("注册信息:{}", userLoginDto);
        adminService.register(userLoginDto);
        return Result.success();
    }

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public Result<UserLoginVO> list(@RequestBody UserLoginDto userLoginDto) {
        log.info("管理员信息:{}", userLoginDto);
        Admin admin = adminService.login(userLoginDto);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, admin.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(admin.getId())
                .username(admin.getUsername())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
    @GetMapping("/logout")
    @ApiOperation("管理员登出")
    public Result logout()
    {
        log.info("管理员登出{}", BaseContext.getCurrentId());

//        userService.logout();
        return Result.success();
    }
    @PostMapping("/updateAdmin")
    @ApiOperation("修改管理员信息")
    public Result update(@RequestBody Admin admin) {
        log.info("修改管理员信息:{}", admin);
        adminService.update(admin);
        return Result.success();
    }
}
